#include <gflags/gflags.h>
#include <thread>
#include "../../common/logger.hpp"
#include "../../common/data_redis.hpp"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(ip, "127.0.0.1", "这是服务器的IP地址，格式：127.0.0.1");
DEFINE_int32(port, 6379, "这是服务器的端口, 格式: 8080");
DEFINE_int32(db, 0, "库的编号：默认0号");
DEFINE_bool(keep_alive, true, "是否进行长连接保活");

void session_test(zrb::Session& session)
{
    session.append("会话ID1","用户ID1");
    session.append("会话ID2","用户ID2");
    session.append("会话ID3","用户ID3");
    session.append("会话ID4","用户ID4");
    session.append("会话ID5","用户ID5");

    session.remove("会话ID2");
    session.remove("会话ID3");
    session.remove("会话ID4");

    auto res1 = session.uid("会话ID1");
    if(res1)
        std::cout << *res1 << std::endl;

    auto res2 = session.uid("会话ID2");
    if(res2)
        std::cout << *res2 << std::endl;

    auto res3 = session.uid("会话ID3");
    if(res3)
        std::cout << *res3 << std::endl;

    auto res4 = session.uid("会话ID4");
    if(res4)
        std::cout << *res4 << std::endl;

    auto res5 = session.uid("会话ID5");
    if(res5)
        std::cout << *res5 << std::endl;
}

void status_test(zrb::Status& status)
{
    status.append("用户ID1");
    status.append("用户ID2");
    status.append("用户ID3");

    status.remove("用户ID2");
    status.remove("用户ID3");

    auto res1 = status.exist("用户ID1");
    if(res1)
        std::cout << "用户ID1存在" << std::endl;

    auto res2 = status.exist("用户ID2");
    if(res2)
        std::cout << "用户ID2存在" << std::endl;

    auto res3 = status.exist("用户ID3");
    if(res3)
        std::cout << "用户ID3存在" << std::endl;
}

void code_test(zrb::Code& code)
{
    code.append("验证码ID1","验证码1");
    code.append("验证码ID2","验证码2");
    code.append("验证码ID3","验证码3");

    code.remove("验证码ID2");

    auto res1 = code.uid("验证码ID1");
    if(res1)
        std::cout << *res1 << std::endl;

    auto res2 = code.uid("验证码ID2");
    if(res2)
        std::cout << *res2 << std::endl;

    auto res3 = code.uid("验证码ID3");
    if(res3)
        std::cout << *res3 << std::endl;

    std::this_thread::sleep_for(std::chrono::seconds(3));

    res1 = code.uid("验证码ID1");
    if(res1)
        std::cout << "验证码ID1不存在" << std::endl;

    res2 = code.uid("验证码ID2");
    if(res2)
        std::cout << "验证码ID2不存在" << std::endl;

    res3 = code.uid("验证码ID3");
    if(res3)
        std::cout << "验证码ID3不存在" << std::endl;
}

int main(int argc,char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    zrb::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    auto client = zrb::RedisClientFactory::create(FLAGS_ip,FLAGS_port,FLAGS_db,FLAGS_keep_alive);
    
    // zrb::Session session(client);
    // session_test(session);

    // zrb::Status status(client);
    // status_test(status);

    zrb::Code code(client);
    code_test(code);

    return 0;
}

